package com.yanqu.road.dao.impl.activity.wanyao;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.wanyao.data.WanYaoUserData;
import com.yanqu.road.entity.activity.wanyao.entity.WanYaoBuffData;
import com.yanqu.road.entity.activity.wanyao.entity.WanYaoLevelData;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class WanYaoUserDaoImpl extends TempDataObjectDao<WanYaoUserData> {

    public static String getWanYaoUserDataSql = "select * from `t_u_wan_yao_user_data` where `activityId`=? and `userId`=?";
    public WanYaoUserData getWanYaoUserData(int activityId,long userId) {
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getWanYaoUserDataSql);
            int i = 1;
            call.setInt(i++, activityId);
            call.setLong(i++, userId);
            rs = call.executeQuery();
            if (rs.next()) {
                return parse(rs);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return null;
    }

    public static String updateWanYaoUserDataSql = "update `t_u_wan_yao_user_data` set monsterPower=?,monsterKingLevel=?,monsterExp=?,buffDataMap=?,power=?,lastPowerRecTime=?,steps=?,flowerNum=?,levelData=?,nowBoardData=?,monsterDataMap=?,hasAddStep=?,totalCostPower=?,hasGetBoxLevel=?,guideStep=? where activityId=? and userId=?";
    @Override
    protected boolean updateData(WanYaoUserData wanYaoUserData) {
        wanYaoUserData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, wanYaoUserData.getMonsterPower()));
        para.put(i++, new DbParameter(Types.INTEGER, wanYaoUserData.getMonsterKingLevel()));
        para.put(i++, new DbParameter(Types.BIGINT, wanYaoUserData.getMonsterExp()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(wanYaoUserData.getBuffDataMap())));
        para.put(i++, new DbParameter(Types.INTEGER, wanYaoUserData.getPower()));
        para.put(i++, new DbParameter(Types.BIGINT, wanYaoUserData.getLastPowerRecTime()));
        para.put(i++, new DbParameter(Types.INTEGER, wanYaoUserData.getSteps()));
        para.put(i++, new DbParameter(Types.BIGINT, wanYaoUserData.getFlowerNum()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(wanYaoUserData.getLevelData())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(wanYaoUserData.getNowBoardData())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(wanYaoUserData.getMonsterDataMap())));
        para.put(i++, new DbParameter(Types.BOOLEAN, wanYaoUserData.getHasAddStep()));
        para.put(i++, new DbParameter(Types.INTEGER, wanYaoUserData.getTotalCostPower()));
        para.put(i++, new DbParameter(Types.INTEGER, wanYaoUserData.getHasGetBoxLevel()));
        para.put(i++, new DbParameter(Types.INTEGER, wanYaoUserData.getGuideStep()));
        para.put(i++, new DbParameter(Types.INTEGER, wanYaoUserData.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, wanYaoUserData.getUserId()));
        if (getDb().executeNoneQuery(updateWanYaoUserDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertWanYaoUserDataSql = "insert into `t_u_wan_yao_user_data`(activityId,userId,monsterPower,monsterKingLevel,monsterExp,buffDataMap,power,lastPowerRecTime,steps,flowerNum,levelData,nowBoardData,monsterDataMap,hasAddStep,totalCostPower,hasGetBoxLevel,guideStep) values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
    @Override
    protected boolean addData(WanYaoUserData wanYaoUserData) {
        wanYaoUserData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, wanYaoUserData.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, wanYaoUserData.getUserId()));
        para.put(i++, new DbParameter(Types.BIGINT, wanYaoUserData.getMonsterPower()));
        para.put(i++, new DbParameter(Types.INTEGER, wanYaoUserData.getMonsterKingLevel()));
        para.put(i++, new DbParameter(Types.BIGINT, wanYaoUserData.getMonsterExp()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(wanYaoUserData.getBuffDataMap())));
        para.put(i++, new DbParameter(Types.INTEGER, wanYaoUserData.getPower()));
        para.put(i++, new DbParameter(Types.BIGINT, wanYaoUserData.getLastPowerRecTime()));
        para.put(i++, new DbParameter(Types.INTEGER, wanYaoUserData.getSteps()));
        para.put(i++, new DbParameter(Types.BIGINT, wanYaoUserData.getFlowerNum()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(wanYaoUserData.getLevelData())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(wanYaoUserData.getNowBoardData())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(wanYaoUserData.getMonsterDataMap())));
        para.put(i++, new DbParameter(Types.BOOLEAN, wanYaoUserData.getHasAddStep()));
        para.put(i++, new DbParameter(Types.INTEGER, wanYaoUserData.getTotalCostPower()));
        para.put(i++, new DbParameter(Types.INTEGER, wanYaoUserData.getHasGetBoxLevel()));
        para.put(i++, new DbParameter(Types.INTEGER, wanYaoUserData.getGuideStep()));
        if (getDb().executeNoneQuery(insertWanYaoUserDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected WanYaoUserData parseData(ResultSet rs) throws SQLException {
        WanYaoUserData wanYaoUserData = new WanYaoUserData();
        wanYaoUserData.setActivityId(rs.getInt("activityId"));
        wanYaoUserData.setUserId(rs.getLong("userId"));
        wanYaoUserData.setMonsterPower(rs.getLong("monsterPower"));
        wanYaoUserData.setMonsterKingLevel(rs.getInt("monsterKingLevel"));
        wanYaoUserData.setMonsterExp(rs.getLong("monsterExp"));
        wanYaoUserData.setBuffDataMap(JSON.parseObject(rs.getString("buffDataMap"), new TypeReference<ConcurrentHashMap<Integer, WanYaoBuffData>>() {}));
        wanYaoUserData.setPower(rs.getInt("power"));
        wanYaoUserData.setLastPowerRecTime(rs.getLong("lastPowerRecTime"));
        wanYaoUserData.setSteps(rs.getInt("steps"));
        wanYaoUserData.setFlowerNum(rs.getLong("flowerNum"));
        wanYaoUserData.setLevelData(JSON.parseObject(rs.getString("levelData"), WanYaoLevelData.class));
        wanYaoUserData.setNowBoardData(JSON.parseObject(rs.getString("nowBoardData"), new TypeReference<ConcurrentHashMap<java.lang.Integer, com.yanqu.road.entity.activity.wanyao.entity.WanYaoBlockData>>() {}));
        wanYaoUserData.setMonsterDataMap(JSON.parseObject(rs.getString("monsterDataMap"), new TypeReference<ConcurrentHashMap<java.lang.Integer, com.yanqu.road.entity.activity.wanyao.entity.WanYaoMonsterData>>() {}));
        wanYaoUserData.setHasAddStep(rs.getBoolean("hasAddStep"));
        wanYaoUserData.setTotalCostPower(rs.getInt("totalCostPower"));
        wanYaoUserData.setHasGetBoxLevel(rs.getInt("hasGetBoxLevel"));
        wanYaoUserData.setGuideStep(rs.getInt("guideStep"));

        return wanYaoUserData;
    }


}
